package world.snowcrystal.configuration;

import org.redisson.Redisson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.messaging.StompSubProtocolErrorHandler;
import world.snowcrystal.properties.ChatroomProperties;

@Configuration
@EnableWebSocketMessageBroker
@ConditionalOnClass(Redisson.class)
@EnableConfigurationProperties(ChatroomProperties.class)
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {

    @Autowired
    private ChannelInterceptor[] interceptors;



    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/snowcrystal-chatroom").setAllowedOrigins("http://localhost:5173","http://localhost:5173");
        registry.setErrorHandler(new StompSubProtocolErrorHandler());
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/channel","/user");

        registry.setApplicationDestinationPrefixes("/chatroom-channel");
        registry.setUserDestinationPrefix("/user");
    }

//    @Override
//    public void configureClientInboundChannel(ChannelRegistration registration) {
//        registration.interceptors(interceptors);
//    }

}
